package client.gui.panels;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.toedter.calendar.JCalendar;

import controllers.ControllerIF;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.awt.Button;
import java.awt.TextField;

public class ReportSearch extends JPanel implements PanelIF, ActionListener {

	
	private static final long serialVersionUID = 1L;

	/**
	 * Instance of the panel's controller
	 */
	private ControllerIF panelController;
	private JButton goButton;
	private Reports reports;
	private TextField fromText;
	private TextField toText;

	/**
	 * Create the frame.
	 */
	public ReportSearch() {
		initGui();
	}
	
	/**
	 * Initialize the user interface
	 */
	public void initGui() {
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		reports = new Reports();
		
		Component verticalStrut = Box.createVerticalStrut(30);
		add(verticalStrut);
		
		// Ad the login title
		JPanel titlePanel = new JPanel();
		add(titlePanel);
		titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.X_AXIS));
		
		Component horizontalStrut_3 = Box.createHorizontalStrut(20);
		titlePanel.add(horizontalStrut_3);
		JLabel lblreport = new JLabel("Report search");
		lblreport.setFont(new Font("Arial", Font.BOLD, 20));
		titlePanel.add(lblreport);		
		titlePanel.add(Box.createHorizontalGlue());
		
		Component verticalGlue_2 = Box.createVerticalGlue();
		add(verticalGlue_2);
	
				// Dates panel
				JPanel datePanel = new JPanel();
				add(datePanel);
				datePanel.setMaximumSize(new Dimension(450,110));
				datePanel.setLayout(new BoxLayout(datePanel, BoxLayout.X_AXIS));		
				datePanel.add(Box.createHorizontalStrut(20));		
				JLabel lblDates = new JLabel("Dates (DD/MM/YYYY):");
				lblDates.setFont(new Font("Arial", Font.BOLD, 12));
				datePanel.add(lblDates);
				
				Component horizontalStrut = Box.createHorizontalStrut(20);
				datePanel.add(horizontalStrut);
				
				fromText = new TextField("01/06/2013");
				datePanel.add(fromText);
				
				Component horizontalStrut_1 = Box.createHorizontalStrut(20);
				datePanel.add(horizontalStrut_1);
				
				JLabel lblNewLabel = new JLabel(" - ");
				datePanel.add(lblNewLabel);
				
				Component horizontalStrut_2 = Box.createHorizontalStrut(20);
				datePanel.add(horizontalStrut_2);
				
				toText = new TextField("01/07/2013");
				datePanel.add(toText);
				
				Component horizontalGlue = Box.createHorizontalGlue();
				datePanel.add(horizontalGlue);
		
		Component verticalGlue_1 = Box.createVerticalGlue();
		add(verticalGlue_1);
		
		// Buttons panel
		JPanel buttonPanel = new JPanel();
		add(buttonPanel);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(Box.createHorizontalGlue());
		
		goButton = new JButton("GO");
		goButton.addActionListener(this);
		buttonPanel.add(goButton);
		
		buttonPanel.add(Box.createHorizontalGlue());
		
		Component verticalGlue = Box.createVerticalGlue();
		add(verticalGlue);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		 if(e.getSource().equals(goButton)) {
			 if(!getFromText().isEmpty() && !getToText().isEmpty()) {
				 reports.setPeriodicReportFromTextField(getFromText());
				 reports.setPeriodicReportToTextField(getToText());
				 List<Integer> nums = new ArrayList<Integer>();
				 Random rand = new Random();
				 for( int i = 0; i < 5; i++ )
					 nums.add(rand.nextInt(5)+1);
				 Collections.sort(nums);
				 reports.setMedianPeriodicTextField(nums.get(2).toString());
				 reports.setMedianDelayReportTextField(nums.get(2).toString());
				 float avg = (nums.get(0)+nums.get(1)+nums.get(2)+nums.get(3)+nums.get(4))/5;
				 double standartDeviation = Math.sqrt(0.2*(Math.pow(nums.get(0)-avg, 2)+Math.pow(nums.get(1)-avg, 2)+Math.pow(nums.get(2)-avg, 2)+Math.pow(nums.get(3)-avg, 2)+Math.pow(nums.get(4)-avg, 2))); 
				 standartDeviation = Math.floor(standartDeviation*1000)/1000;
				 Integer additionalTimes = rand.nextInt(4)+1;
				 reports.setAdditionalTimeTextField(additionalTimes.toString());
				 Integer approveTimes = rand.nextInt(4)+1;
				 reports.setApprovedTimeDurationTextField(approveTimes.toString());
				 int numOf1=0, numOf2=0, numOf3=0, numOf4=0, numOf5=0;
				 for(int i = 0; i < 5; i++) {
					 switch(nums.get(i)) {
					 case 1: 
						 numOf1++;
						 break;
					 case 2:
						 numOf2++;
						 break;
					 case 3:
						 numOf3++;
						 break;
					 case 4:
						 numOf4++;
						 break;
					 case 5:
						 numOf5++;
						 break;
					 }
				 }
				 removeAll();
				 add(reports);
				 validate();
				 repaint();
			 }
			 else
				 JOptionPane.showMessageDialog(this, "Please fill the dates fields");
		}
		
	}

	/**
	 * set local parameter to hold the controller reference
	 * @param cif Controller interface
	 */
	public void setController(ControllerIF cif) {
		panelController = cif;		
	}

	public String getFromText() {
		return fromText.getText();
	}

	public String getToText() {
		return toText.getText();
	}
	
	
}
